<template>
    <el-form :model="form" label-width="auto" style="max-width: 600px">
        <tr>
            <td>
                <el-form-item label="任务编码">
                    <el-input v-model="form.tasksCode" disabled />
                </el-form-item>
            </td>
            <td>
                <el-form-item label="任务名称">
                    <el-input v-model="form.tasksName" />
                </el-form-item>
            </td>
        </tr>
        <tr>
            <td>
                <el-form-item label="任务类型">
                    <el-select v-model="form.tasksType" placeholder="请选择">
                        <el-option label="标准" :value=1 />
                        <el-option label="非标准" :value=2 />
                    </el-select>
                </el-form-item>
            </td>
            <td>
                <el-form-item label="任务状态">
                    <el-select v-model="form.tasksState" disabled placeholder="请选择">
                        <el-option label="已开放" :value=1 />
                    </el-select>
                </el-form-item>
            </td>
        </tr>
        <tr>
            <td>
                <el-form-item label="物料">
                    <el-select v-model="form.chooseId" placeholder="请选择">
                        <el-option v-for="a in inventoryInfo" :label="a.chooseName" :value="a.chooseId"
                            @click="getspec(), getunit(), getMangItemManifestInfo(), getProcessRoute()" />
                    </el-select>
                </el-form-item>
            </td>
            <td>
                <el-form-item label="物料规格">
                    <el-select v-model="form.materialSpecsId" disabled placeholder="请选择">
                        <el-option label="50*1.8" :value=1 />
                        <el-option label="50*1.5" :value=2 />
                        <el-option label="50*1.4" :value=3 />
                    </el-select>
                </el-form-item>
            </td>
        </tr>

        <tr>
            <td>
                <el-form-item label="物料单位">
                    <el-select v-model="form.itemUnitId" disabled placeholder="请选择">
                        <el-option label="克" :value=1 />
                        <el-option label="千克" :value=2 />
                        <el-option label="吨" :value=3 />
                    </el-select>
                </el-form-item>
            </td>
            <td>
                <el-form-item label="生产数量">
                    <el-input v-model="form.productionNum" />
                </el-form-item>
            </td>
        </tr>
        <tr>
            <td>
                <el-form-item label="开工时间">
                    <el-col :span="11">
                        <el-date-picker v-model="form.startTime" type="date" placeholder="请选择" style="width: 100%" />
                    </el-col>
                </el-form-item>
            </td>
            <td>
                <el-form-item label="完工时间">
                    <el-col :span="11">
                        <el-date-picker v-model="form.completionTime" type="date" placeholder="请选择" style="width: 100%" />
                    </el-col>
                </el-form-item>
            </td>
        </tr>
        <h3>对应物料清单</h3>
        <tr>
            <td>
                <el-form-item label="物料清单">
                    <el-input v-model="form.itemManifestCode">
                    </el-input>
                </el-form-item>
            </td>
            <td>
                <el-form-item label="物料清单版本">
                    <el-input v-model="form.itemManifestVersion" disabled />
                </el-form-item>
            </td>
        </tr>
        <h3>对应工艺路线</h3>
        <tr>
            <td>
                <el-form-item label="工艺路线">
                    <el-input v-model="form.processRouteNo" />
                </el-form-item>
            </td>
            <td>
                <el-form-item label="工艺路线版本">
                    <el-input v-model="form.processVersion" disabled />
                </el-form-item>
            </td>
        </tr>
        <h3></h3>
        <tr>
            <td>
                <el-form-item label="生产入库数量">
                    <el-input v-model="form.inboundQuantity" disabled />
                </el-form-item>
            </td>
            <td>
                <el-form-item label="生产退货数量">
                    <el-input v-model="form.numberReturns" disabled />
                </el-form-item>
            </td>
        </tr>
        <tr>
            <td>
                <el-form-item label="制单人">
                    <el-input v-model="form.peole" disabled />
                </el-form-item>
            </td>
            <td>
                <el-form-item label="制单部门">
                    <el-select v-model="form.departmentId" placeholder="请选择">
                        <el-option label="JAVA组" :value=1 />
                    </el-select>
                </el-form-item>
            </td>
        </tr>
        <!-- <el-button type="success" @click="add">修改</el-button> -->

        <el-button type="success" style="width: 83px;float: right;margin: 10px;"
            v-show="form.pid == 0 || form.pid == 3" @click="add">保存</el-button>
        <el-button type="primary" style="width: 83px;float: right;margin: 10px;"
            v-show="form.pid == 0 || form.pid == 3" @click="Submit1()">提交</el-button>
        <el-button type="warning" style="width: 83px;float: right;margin: 10px;" v-show="form.pid == 1"
            @click="Submit()">审核通过</el-button>
        <el-button type="danger" style="width: 83px;float: right;margin: 10px;" v-show="form.pid == 1"
            @click="Overrule()">驳回</el-button>
        <el-button type="success" style="width: 83px;float: right;margin: 10px;" v-show="form.pid == 2"
            @click="add">变更</el-button>
    </el-form>
    <!-- 弹框新增 -->
    <el-button @click="getlist()">生成物料需求</el-button>
    <el-table :data="list" style="width: 100%">
        <el-table-column prop="rowId" label="序号" />
        <el-table-column prop="rowOrder" label="顺序" />
        <el-table-column prop="process" label="工序" />
        <el-table-column prop="chooseId" label="物料名称">
            <template v-slot="scope">
                <span v-if="scope.row.chooseId == 1">棉花</span>
                <span v-else>羊皮</span>
            </template>
        </el-table-column>
        <el-table-column prop="rowId" label="单位">
            <template v-slot="scope">
                <span v-if="scope.row.rowId == 1">次</span>
                <span v-else-if="scope.row.rowId == 2">克</span>
                <span v-else>委外</span>
            </template>
        </el-table-column>
        <el-table-column prop="specName" label="规格" />
        <!-- <el-table-column prop="chooseId" label="物料属性">
            <template v-slot="scope">
                <span v-if="scope.row.chooseId == 1">自制</span>
                <span v-else-if="scope.row.chooseId == 2">采购</span>
                <span v-else>委外</span>
            </template>
        </el-table-column> -->
        <el-table-column prop="num" label="所需数量" />
        <el-table-column prop="planningNum" label="领用数量" />
        <el-table-column prop="date" label="返回数量" />
        <el-table-column prop="leadPeriod" label="前置期">

        </el-table-column>

        <el-table-column prop="pid" label="下达状态">
            <template v-slot="scope">
                <span v-if="scope.row.pid == 1">已发放</span>
                <span v-else>未发放</span>
            </template>
        </el-table-column>
        <el-table-column label="操作">
            <template v-slot="scope">
                <el-button>删除</el-button>
                <el-button>编辑</el-button>
            </template>
        </el-table-column>
    </el-table>
</template>
<script setup lang="ts">
import { reactive, ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter, useRoute } from 'vue-router'
import type { FormInstance } from 'element-plus'
import axios from 'axios'
const apiurls = import.meta.env.VITE_APP_API_URL_ProductionAdd;
const apiurl = import.meta.env.VITE_APP_API_URL_ProductionShow;
const router = useRouter()
const route = useRoute()
const formRef = ref<FormInstance>()
const form = reactive({
    tasksId: route.query.tasksId,
    tasksCode: "",
    tasksName: "",
    tasksType: "",
    tasksState: 1,
    chooseId:  route.query.chooseId,
    materialSpecsId: "",
    itemUnitId: "",
    productionNum: "",
    startTime: "",
    completionTime: "",
    itemManifestCode: "",
    itemManifestVersion: "",
    processRouteNo: "",
    processVersion: "",
    inboundQuantity: "",
    numberReturns: "",
    peole: "Admin",
    departmentId: 1,
    handle: "Admin",
    handleContext: "新增",
    handleTime: "",
    pid: 1,
    isDelete: false
});
const formm = reactive({
    planningRow: 0,
    chooseId: 0,
    attributeRowId: 0,
    unitName: "",
    spec: "",
    planningNum: 0,
    planningStartDate: "",
    planningEndDate: "",
    markment: "",
    handle: "MaxAdmin",
    handleContext: "新增",
    handleTime: "",
    pid: 1,
    isDelete: true
})
//弹框新增
const ti = () => {
    axios.post(apiurls + '/api/Material/AddRowMaterialPlanning', {
        planningRow: formm.planningRow,
        chooseId: formm.chooseId,
        attributeRowId: formm.attributeRowId,
        unitName: formm.unitName,
        spec: formm.spec,
        planningNum: formm.planningNum,
        planningStartDate: formm.planningStartDate,
        planningEndDate: formm.planningEndDate,
        markment: formm.markment,
        IsDelete: false,
        handle: formm.handle,
        handleContext: formm.handleContext,
        handleTime: new Date(),
        pid: 1
    }).then(res => {
        if (res.data.code > 0) {
            console.log(res.data)
            ElMessage.success("添加成功")
            form.dialogFormVisible = false
        } else {
            console.log(res.data)
            ElMessage.error("添加失败")
        }
    })
}
//行显示
const list = ref([])
const getlist = () => {
    axios.post(apiurl + '/api/ShowMaterial/GetRowTask', {
        chooseId:form.chooseId
    }).then(res => {
        console.log(res.data)
        list.value = res.data.data

    })
}
//物料选择下拉框
const inventoryInfo = ref([])
const getinventoryInfo = () => {
    axios.post(apiurl + '/api/ShowMaterial/GetSelection', {}).then(res => {
        inventoryInfo.value = res.data.data
    })
}
//单位反填
const getunit = () => {
    axios.post(apiurl + '/api/ShowMaterial/GetItemUnit', {
        chooseId: form.chooseId
    }).then(res => {
        form.itemUnitId = res.data.data.itemUnitId
    })
}
//规格反填
const getspec = () => {
    axios.post(apiurl + '/api/ShowMaterial/GetSpecs', {
        chooseId: form.chooseId
    }).then(res => {
        form.materialSpecsId = res.data.data.materialSpecsId
    })
}
//物料清单反填
const getMangItemManifestInfo = () => {
    axios.post(apiurl + '/api/ShowMaterial/MangItemManifestInfo', {
        chooseId: form.chooseId
    }).then(res => {
        form.itemManifestCode = res.data.data.itemManifestCode
        form.itemManifestVersion = res.data.data.itemManifestVersion
    })
}
//工艺路线反填
const getProcessRoute = () => {
    axios.post(apiurl + '/api/ShowMaterial/MangProcessRouteMangCommon', {
        inventoryInfoId: form.chooseId
    }).then(res => {
        form.processRouteNo = res.data.data.processRouteName
        form.processVersion = res.data.data.processVersion
    })
}
//生产任务返填
const getTasks = () => {
    axios.post(apiurl + '/api/ShowMaterial/TaskMang', {
        tasksId: form.tasksId
    }).then(res => {
        form.tasksId = res.data.data.tasksId
        form.tasksCode = res.data.data.tasksCode
        form.tasksName = res.data.data.tasksName
        form.tasksType = res.data.data.tasksType
        form.tasksState = res.data.data.tasksState
        form.chooseId = res.data.data.chooseId
        form.materialSpecsId = res.data.data.materialSpecsId
        form.itemUnitId = res.data.data.itemUnitId
        form.productionNum = res.data.data.productionNum
        form.startTime = res.data.data.startTime

        form.completionTime = res.data.data.completionTime
        form.itemManifestCode = res.data.data.itemManifestCode
        form.itemManifestVersion = res.data.data.itemManifestVersion
        form.processRouteNo = res.data.data.processRouteNo

        form.processVersion = res.data.data.processVersion
        form.inboundQuantity = res.data.data.inboundQuantity
        form.numberReturns = res.data.data.numberReturns
        form.peole = res.data.data.peole
        form.departmentId = res.data.data.departmentId
        form.handle = res.data.data.handle

        form.handleContext = res.data.data.handleContext
        form.handleTime = res.data.data.handleTime
        form.pid = res.data.data.pid
        form.isDelete = res.data.data.isDelete
    })
}

//修改生产任务
const add = () => {
    axios.post(apiurls + '/api/Material/UpdateTasks', {
        tasksId: form.tasksId,
        tasksCode: form.tasksCode,
        tasksName: form.tasksName,
        tasksType: form.tasksType,
        tasksState: form.tasksState,
        chooseId: form.chooseId,
        materialSpecsId: form.materialSpecsId,
        itemUnitId: form.itemUnitId,
        productionNum: Number(form.productionNum),
        startTime: form.startTime,
        completionTime: form.completionTime,
        itemManifestCode: form.itemManifestCode,
        itemManifestVersion: form.itemManifestVersion,
        processRouteNo: form.processRouteNo,
        processVersion: form.processVersion,
        inboundQuantity: 0,
        numberReturns: 0,
        peole: form.peole,
        departmentId: 1,
        IsDelete: false,
        handle: form.handle,
        handleContext: form.handleContext,
        handleTime: new Date(),
        pid: form.pid
    }).then(res => {
        if (res.data.code > 0) {
            console.log(res.data)
            ElMessage.success("修改成功")
            router.push({ name: 'ProductionTasksList' })
        } else {
            console.log(res.data)
            ElMessage.error("修改失败")
        }
    })
}

//审批
const Submit=()=>{
    axios.post(apiurls+'/api/Material/UpdaState',form).then((res)=>{
        if(res.data.code==1){
            ElMessage({
                message: '审核通过',
                type: 'success',
            })
            router.push({ name: 'ProductionTasksList' })
        }else{
            ElMessage.error(res.data.msg)
        }
    })
}

//提交
const Submit1=()=>{
    axios.post(apiurls+'/api/Material/UpdaTaskSubmit',form).then((res)=>{
        if(res.data.code==1){
            ElMessage({
                message: '提交成功',
                type: 'success',
            })
            router.push({ name: 'ProductionTasksList' })
        }else{
            ElMessage.error(res.data.msg)
        }
    })
}

//驳回
const Overrule=()=>{
    axios.post(apiurls+'/api/Material/UpdaTaskOverrule',form).then((res)=>{
        if(res.data.code==1){
            ElMessage({
                message: '已驳回',
                type: 'success',
            })
            router.push({ name: 'ProductionTasksList' })
        }else{
            ElMessage.error(res.data.msg)
        }
    })
}



onMounted(() => {
    getTasks()
    getinventoryInfo()
    getlist()
})
</script>
